<template>
    <el-dialog v-model="dialogVisible" title="设备数据上传" :close-on-click-modal="false" append-to-body width="800"
        class="editDialog" align-center>


        <el-table :data="adList" border row-key="id" style="width: 100%; margin-top: 20px"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column header-align="center" prop="snCode" label="设备号" />
            <el-table-column header-align="center" prop="adNo" label="通道号" width="60" />

            <el-table-column header-align="center" prop="quantityName" label="物理量">
                <template #default="scope">
                    <el-input v-model="scope.row.quantityName" />
                </template>
            </el-table-column>

            <el-table-column header-align="center" prop="time" label="时间">
                <template #default="scope">
                    <el-input v-model="scope.row.time" />
                </template>
            </el-table-column>

            <el-table-column header-align="center" prop="data" label="数据">
                <template #default="scope">
                    <el-input v-model="scope.row.data" />
                </template>
            </el-table-column>
        </el-table>


        <template #footer>
            <el-button @click="dialogVisible = false" auto-insert-space>取消</el-button>

            <el-button type="primary" @click="handleSave" auto-insert-space>保存</el-button>

            <el-button type="primary" @click="onConfirm" auto-insert-space>批量导入</el-button>
        </template>

    </el-dialog>
</template>

<script lang="js" setup>

import { ref, defineProps, reactive } from "vue";

import { ElMessage } from "element-plus";

import { equipmentAdList, equipmentAdSaveData, equipmentAdImpData } from "@/utils/requests";

let dialogVisible = ref(false);

// eslint-disable-next-line no-undef,no-unused-vars
const emits = defineEmits(["confirm"]);

const adList = ref([]);

const openDialog = (rowData) => {
    dialogVisible.value = true;

    adList.value = [];

    search(rowData);
};

const search = (rowData) => {
    equipmentAdList({ snCode: rowData.mac, size: 50, current: 1 }).then((res) => {
        adList.value = res.records.map(item => {
            return { snCode: item.snCode, adNo: item.adNo, quantityName: "", time: "", data: null }
        })
    })
}

const handleSave = () => { 
    equipmentAdSaveData([...adList.value]).then((res) => {

        // adList.value = adList.value.map(item => {
        //     return { snCode: item.snCode, adNo: item.adNo, quantityName: "", time: "", data: null }
        // })

        ElMessage.success("保存成功");
    }).catch((ex) => {

        ElMessage.success("保存失败");
    })
}
defineExpose({ openDialog });

</script>


<style scoped lang="scss">
.basicForm,
.selectionForm {
    >* {
        width: calc(50% - 20px);
        margin-right: 20px;

        .selectInput {
            flex: 1;
        }

        :deep(.el-date-editor) {
            width: 100%;
        }
    }

    >.long {
        width: calc(100% - 20px);
        margin-right: 20px;
    }

    .sumUnit {
        .text {
            margin-right: 10px;
        }
    }
}
</style>